package com.baosight.sczx.zm.cn.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cn.domain.ZMCNCcyMain;
import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;

/**
 * 内部抽查样主表
 * <p>Title: ServiceZMCN30.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCN30 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCN10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCN10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		EiInfo outInfo = super.query(inInfo, "ZMCNCcyMain.query", new ZMCNCcyMain());
		
		outInfo.addBlock(getPartDept()); 
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		//D.10223代表质检科
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,"D.10223"));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值
//		outInfo.set("fdCjdw", UserSession.getInSessionProperty("orgCode"));//单位的默认值
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		outInfo.addBlock(getCoalCode());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cn.mineType"));
		return outInfo;
	}

	/**
	 * 煤样编码下拉框
	 * @return
	 */
	public EiBlock getCoalCode() {
		EiBlock block = new EiBlock("cmes.ch.coalcode");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			Map<String, String> params=new HashMap();
			params.put("fdStatus", "生效");
			_listData = dao.query("ZMCNMcmodule.query", params);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_map = new HashMap<String, String>();
			ZMCNMcmodule _temp = (ZMCNMcmodule)_listData.get(i);
			_map.put("value", _temp.getFdMyCode());
			_map.put("display",_temp.getFdMyDetail());
			_list.add(_map);
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 得到质检中心下属分站下拉框
	 * @return
	 */
	public EiBlock getPartDept() {
		EiBlock block = new EiBlock("cmes.ch.partdept");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz001");
		_map.put("display", "中心站");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz002");
		_map.put("display", "松藻站");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz003");
		_map.put("display", "白岩站");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz004");
		_map.put("display", "金鸡岩站");
		_list.add(_map);

		block.setRows(_list);
		return block;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCNCcyMain _dom = new ZMCNCcyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
//			params.put("fdMiningName", _dom.getFdMiningName());
//			params.put("fdYear", _dom.getFdYear());
//			boolean validate = this.validate(inInfo, _dom, params);
//			if(validate){
//				return inInfo;
//			}
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_CCY_MAIN"));
			this.dao.insert("ZMCNCcyMain.insert", _dom);
	    }
	 	out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
 }

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCNCcyMain _dom = new ZMCNCcyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCNCcyMain.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{

		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCNCcyMain.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCNCcyMain _dom = new ZMCNCcyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			this.dao.update("ZMCNCcyMain.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	

	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNCcyMain _dom = new ZMCNCcyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");

		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			String fdCjnr = _dom.getFdCjnr();
		//	String fdCjdw = _dom.getFdCjdw();
		//	String fdProjectName = _dom.getFdProjectName();
			String fdYear = _dom.getFdYear();
			String fdMonth = _dom.getFdMonth();
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			params.put("fdCjnr", fdCjnr);
	//		params.put("fdCjdw", fdCjdw);
	//		params.put("fdProjectName", fdProjectName);
			params.put("fdYear", fdYear);
			params.put("fdMonth", fdMonth);
			List result = dao.query("ZMCNCcyMain.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNCcyMain _temp = (ZMCNCcyMain)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdYear() != null && _temp.getFdYear().equals(fdYear)) {
					msg.append(" 年份 ");
				}
				if(_temp.getFdMonth()!= null && _temp.getFdMonth().equals(fdMonth)) {
					msg.append(" 月份 ");
				}
				if(_temp.getFdCjnr() != null && _temp.getFdCjnr().equals(fdCjnr)) {
					msg.append(" 抽检内容 ");
				}
/*				if(_temp.getFdCjdw() != null && _temp.getFdCjdw().equals(fdCjdw)) {
					msg.append(" 抽检单位 ");
				}
				if(_temp.getFdProjectName() != null && _temp.getFdProjectName().equals(fdProjectName)) {
					msg.append(" 项目名称 ");
				}*/
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate1</p>
	 * <p>Description: 明细上报不能修改主表，有明细不能删除主表。</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-23 17:09:36 lhx
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate1(EiInfo inInfo) {
		ZMCNCcyMain _dom = new ZMCNCcyMain();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			String fdYear = _dom.getFdYear();
			String fdMonth = _dom.getFdMonth();
			String fdCjnr = _dom.getFdCjnr();
			
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdStatus", "上报");
			}
			params.put("fdYear", fdYear);
			params.put("fdMonth", fdMonth);
			params.put("fdCjnr", fdCjnr);
			
			List result = dao.query("ZMCNCcy.validate1", params);
	//		EiInfo out=new EiInfo();
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
				{
					msg.append("明细已经上报，不能修改主表！");
				}else{
					msg.append("明细有记录。为了防止误删除，请先删除明细后再删除主表！");
				}
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
}
